package com.pyg.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pyg.manager.service.SpecificationService;
import com.pyg.utils.PageResult;
import com.pyg.utils.PygResult;
import com.pyg.utils.Specification;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by on 2018/8/10.
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {

    @Reference(timeout = 1000000)
    private SpecificationService specificationService;

    @RequestMapping("/findByPage/{page}/{rows}")
    public PageResult findByPage(@PathVariable Integer page, @PathVariable Integer rows) {
        //调用远程服务对象方法
        PageResult result = specificationService.findByPage(page,rows);
        return result;
    }
    @RequestMapping("add")
    public PygResult add(@RequestBody Specification specification) {
        //调用远程服务对象方法
        try {
            specificationService.add(specification );
            return new PygResult(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(true, "增加失败");
        }
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @RequestMapping("/findOne/{id}")
    public Specification findOne(@PathVariable Long id) {
        //调用远程服务对象方法
        return specificationService.findOne(id);
    }
    @RequestMapping("/update")
    public PygResult update(@RequestBody Specification specification) {
        //调用远程服务对象方法
        try {
            specificationService.update(specification);
            return new PygResult(true, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(true, "更新失败");
        }
    }
    @RequestMapping("/delete/{ids}")
    public PygResult del(@PathVariable Long[] ids) {
        //调用远程服务对象方法
        try {
            specificationService.del(ids);
            return new PygResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new PygResult(true, "删除失败");
        }
    }

    //定义一个规格select2接口的方法
    @RequestMapping("/findSpecificationList")
    public List<Map> findSpecificationList() {
        return specificationService.findSpecificationList();
    }
}
